#!/bin/ksh

#--------------------------------------------------------------------------
#
# LINUX Only.
#
# Main script to configure websm.  Typically called from an rpm package
#
#--------------------------------------------------------------------------

# Check whether we need to do security specific work

if [ $# -gt 0 ] ; then

  if [ "$1" = "Security" ] ; then
     mkdir -p /var/websm/security
     exit $?
  fi
  exit 
fi 

# Add websm to the inittab
if [ -f /usr/websm/bin/rc.websm ]; then
   /usr/websm/bin/rc.websm -enable
fi

# Configure websm to work with pam
if [ -f /usr/websm/bin/wsmpamconfig ]; then
   /usr/websm/bin/wsmpamconfig -enable
fi

# Configure websm to work with pam
if [ -f /usr/websm/bin/wsmserver ]; then
   /usr/websm/bin/wsmserver -enable
fi

#
# Symbolic links needed
#
if [ -d /usr/websm/html ] ; then
   ln -s /usr/websm                        /usr/websm/html/wsmship
   ln -s /usr/websm/codebase               /usr/websm/html/codebase
fi

# Add websm and related directories in var 
if [ ! -d /var/websm/config/user_settings ]; then
   mkdir -p /var/websm/config/user_settings
fi

# Add directory for addition jars
if [ ! -d /usr/websm/codebase/pluginjars ]; then
   mkdir -p /usr/websm/codebase/pluginjars
fi

if [ ! -f /var/websm/config/user_settings/websm.cfg ]; then
   cp /usr/websm/config/factory_defaults/websm.cfg /var/websm/config/user_settings
fi

#ln -s /usr/lib/java/taskguide/rsd       /usr/websm/codebase

#ln -s /usr/lib/java/taskguide/sguide    /usr/websm/codebase
#ln -s /usr/lib/java/taskguide           /usr/websm/codebase

ln -s /usr/websm/codebase/taskguide/sguide    /usr/websm/codebase
ln -s /var/websm/config/user_settings/websm.cfg /usr/websm/websm.cfg
ln -s /websm.script                     /var/websm/websm.script
ln -s /usr/websm/bin/wsmoutput          /usr/websm/bin/wsmrefresh
ln -s /usr/websm/bin/configassist       /usr/bin/configassist
ln -s /usr/websm/bin/wsm                /usr/bin/wsm

if [  -d ./opt -a ! -f ./opt/websm ] ; then
ln -s /usr/websm                /opt/websm
fi

#
# Additional post install configurations taken from websm.framework.post_i
#


#--------------------------------------------------------------------------
#
# Begining of if block for testing the /usr/websm/websm.cfg.saved file
#
#--------------------------------------------------------------------------

if [[ -f /usr/websm/websm.cfg.saved ]] && [[ ! -f /usr/websm/config/user_settings/websm.cfg ]]
then
  /usr/bin/cp /usr/websm/websm.cfg.saved                            \
        /var/websm/config/user_settings/websm.cfg > /dev/null 2>&1

        if [[ $? != 0 ]] then
        exit $?
        fi
fi

# Ending of if block for testing the /usr/websm/websm.cfg.saved file


#------------------------------------------------------
#
# Begining of if block for testing the
# /var/websm/config/user_settings/websm.cfg file.
#
#------------------------------------------------------

if [[ -f /var/websm/config/user_settings/websm.cfg ]]
 then
  /bin/grep "forcessl" /var/websm/config/user_settings/websm.cfg \
                                        > /tmp/force_tmp.$$
  if [[ $? != 0 ]]
  then
  exit $?
  fi

     if [[ -s /tmp/force_tmp.$$ ]]
     then
         ( /bin/cat /var/websm/config/user_settings/websm.cfg        \
            | /bin/grep -v "forcessl" |/bin/grep -v "#######"  \
            | /bin/grep -v "# The following"                       \
            |/bin/grep -v "# and user_settings"                    \
                    >  /tmp/f_def_wcfg1.$$ || true) 2>/dev/null

         if [[ $? != 0 ]]
         then
         exit $?
         fi

         ( /bin/cat /usr/websm/config/factory_defaults/websm.cfg   \
                      | /bin/grep -v "forcessl"                  \
                      | /bin/grep -v "#"                         \
                      >  /tmp/f_def_wcfg2.$$                         \
                      || true ) 2>/dev/null

         if [[ $? != 0 ]]
         then
         exit $?
         fi
         (/bin/cat /usr/websm/config/factory_defaults/websm.cfg   \
                      | /bin/grep  "#"  |sort -u                 \
                      >  /tmp/f_def_wcfg3.$$                         \
                      || true ) 2>/dev/null

         if [[ $? != 0 ]]
         then
         exit $?
         fi

         for i in `cat /tmp/f_def_wcfg1.$$`
           do
             /bin/grep $i /tmp/f_def_wcfg2.$$                    \
             >> /tmp/f_def_wcfg4.$$ || true 2>/dev/null
           done
           set +e
           /usr/bin/diff /tmp/f_def_wcfg4.$$ /tmp/f_def_wcfg2.$$     \
                                        > /tmp/f_def_wcfg5.$$
           set +e
           /bin/grep  ">" /tmp/f_def_wcfg5.$$ | cut -d ">" -f 2  \
                           | cut -d " " -f 2 > /tmp/f_def_wcfg6.$$

         if [[ -f "/usr/websm/security/SM.privkr" ]]
             then
              /bin/echo "#################################################################" > /var/websm/config/user_settings/websm.cfg
              /bin/echo "# The following entries are created by merging factory_defaults" >> /var/websm/config/user_settings/websm.cfg
              /bin/echo "# and user_settings files as a part of post installation of WebSM." >> /var/websm/config/user_settings/websm.cfg
              /bin/echo "#################################################################" >> /var/websm/config/user_settings/websm.cfg

                 /bin/cat /tmp/f_def_wcfg6.$$                    \
                     >> /var/websm/config/user_settings/websm.cfg    \
                                         2>/dev/null

                 /bin/cat /tmp/f_def_wcfg1.$$                    \
                     >> /var/websm/config/user_settings/websm.cfg    \
                                         2>/dev/null
          /bin/echo "forcessl=true"                              \
                    >> /var/websm/config/user_settings/websm.cfg


         else
              /bin/echo "#################################################################" > /var/websm/config/user_settings/websm.cfg
              /bin/echo "# The following entries are created by merging factory_defaults" >> /var/websm/config/user_settings/websm.cfg
              /bin/echo "# and user_settings files as a part of post installation of WebSM." >> /var/websm/config/user_settings/websm.cfg
              /bin/echo "#################################################################" >> /var/websm/config/user_settings/websm.cfg

                 /bin/cat /tmp/f_def_wcfg6.$$                    \
                     >> /var/websm/config/user_settings/websm.cfg    \
                                         2>/dev/null

                 /bin/cat /tmp/f_def_wcfg1.$$                    \
                     >> /var/websm/config/user_settings/websm.cfg    \
                                         2>/dev/null

             /bin/echo "forcessl=false"                        \
                       >> /var/websm/config/user_settings/websm.cfg

         fi
         else
             /bin/cat /var/websm/config/user_settings/websm.cfg    \
             | /bin/grep -v "forcessl" |/bin/grep -v "#######" \
             | /bin/grep -v "# The following"                      \
             |/bin/grep -v "# and user_settings"                   \
                    >  /tmp/f_def_wcfg1.$$ || true 2>/dev/null

             if [[ $? != 0 ]]
             then
             exit $?
             fi

             (/bin/cat /usr/websm/config/factory_defaults/websm.cfg \
                           | /bin/grep -v "#"                      \
                           >  /tmp/f_def_wcfg2.$$                      \
                           || true ) 2>/dev/null

             if [[ $? != 0 ]]
             then
             exit $?
             fi

             (/bin/cat /usr/websm/config/factory_defaults/websm.cfg \
                          | /bin/grep  "#" |sort -u                \
                          >  /tmp/f_def_wcfg3.$$                       \
                         || true ) 2>/dev/null

             if [[ $? != 0 ]]
             then
             exit $?
             fi
             for i in `cat /tmp/f_def_wcfg1.$$`
               do
                 /bin/grep $i /tmp/f_def_wcfg2.$$                  \
                        >> /tmp/f_def_wcfg4.$$ || true 2>/dev/null
              done

              set +e
              /usr/bin/diff /tmp/f_def_wcfg4.$$ /tmp/f_def_wcfg2.$$    \
                                        > /tmp/f_def_wcfg5.$$
              set -e
              /bin/grep  ">" /tmp/f_def_wcfg5.$$ | cut -d ">" -f 2 \
                           |cut -d " " -f 2 > /tmp/f_def_wcfg6.$$

              /bin/echo "#################################################################" > /var/websm/config/user_settings/websm.cfg
              /bin/echo "# The following entries are created by merging factory_defaults" >> /var/websm/config/user_settings/websm.cfg
              /bin/echo "# and user_settings files as a part of post installation of WebSM." >> /var/websm/config/user_settings/websm.cfg
              /bin/echo "#################################################################" >> /var/websm/config/user_settings/websm.cfg

                 /bin/cat /tmp/f_def_wcfg6.$$                    \
                     >> /var/websm/config/user_settings/websm.cfg    \
                                         2>/dev/null
                 /bin/cat /tmp/f_def_wcfg1.$$                    \
                     >> /var/websm/config/user_settings/websm.cfg    \
                                         2>/dev/null

                 /bin/echo "forcessl=false"                      \
                       >> /var/websm/config/user_settings/websm.cfg

     fi

     else
         /bin/cp -p /usr/websm/config/factory_defaults/websm.cfg   \
         /var/websm/config/user_settings/websm.cfg

         if [[ $? != 0 ]]
         then
         exit $?
         fi


fi
 /bin/rm -f /tmp/f_def_wcfg1.$$
 /bin/rm -f /tmp/f_def_wcfg2.$$
 /bin/rm -f /tmp/f_def_wcfg3.$$
 /bin/rm -f /tmp/f_def_wcfg4.$$
 /bin/rm -f /tmp/f_def_wcfg5.$$
 /bin/rm -f /tmp/f_def_wcfg6.$$
 /bin/rm -f /tmp/force_tmp.$$



